package com.jarvis.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jarvis.common.utils.PageUtils;
import com.jarvis.common.utils.Query;

import com.jarvis.user.dao.UserDao;
import com.jarvis.user.entity.UserEntity;
import com.jarvis.user.service.UserService;


@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements UserService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<UserEntity> page = this.page(
                new Query<UserEntity>().getPage(params),
                new QueryWrapper<UserEntity>()
        );

        return new PageUtils(page);
    }


    
    @Override
    public  UserEntity login(String deviceId){

        UserDao userDao = this.baseMapper;
        long count = baseMapper.selectCount(new QueryWrapper<UserEntity>()
                .eq("device_id", deviceId));
        System.out.print(deviceId);
        if(count > 0) {
            return  userDao.selectOne(new QueryWrapper<UserEntity>().eq("device_Id",deviceId));
        }else {

            Date creatDate = new Date();
            UserEntity userEntityntity = new UserEntity();
            userEntityntity.setDeviceId(deviceId);
            userEntityntity.setCreateTime(creatDate);
            userEntityntity.setFreecount(5);
            userEntityntity.setLevelId(Long.parseLong("1"));
            userEntityntity.setUsername("");
            userEntityntity.setPassword("");
            userEntityntity.setSalt("");
            userEntityntity.setNickname("");
            userEntityntity.setPhone("");
            userEntityntity.setEmail("");
            userEntityntity.setHeader("");
            userEntityntity.setGender(1);
            userEntityntity.setBirthday(creatDate);
            userEntityntity.setCity("");
            userEntityntity.setJob("");
            userEntityntity.setSign("");
            userEntityntity.setSourceType(1);
            userEntityntity.setEndTime(creatDate);
            userEntityntity.setStatus(1);
            userEntityntity.setAppleId("");
            userEntityntity.setWxId("");
            userEntityntity.setVip(0);
            userDao.insert(userEntityntity);
            return userEntityntity;
        }

    }


    @Override
    public UserEntity getUser(long userId){
        UserDao userDao = this.baseMapper;
        QueryWrapper<UserEntity> queryWrapper =  new QueryWrapper<UserEntity>().eq("id",userId);
        return userDao.selectOne(queryWrapper);
    }

    @Override
    public  void  reduceUserFreecount(long userId){
        UserDao userDao = this.baseMapper;
        QueryWrapper<UserEntity> queryWrapper =  new QueryWrapper<UserEntity>().eq("id",userId);
        UserEntity entity =  userDao.selectOne(queryWrapper);
        //获取用户实体的免费次数 -1
        int freeCount = entity.getFreecount();
        if (freeCount > 0 ){
            freeCount -=1 ;
        }else {
            freeCount = 0;
        }
        entity.setFreecount(freeCount);
        userDao.update(entity,new UpdateWrapper<UserEntity>());

    }

    @Override
    public  void  addUserFreecount(long userId,int count) {

        UserDao userDao = this.baseMapper;
        QueryWrapper<UserEntity> queryWrapper =  new QueryWrapper<UserEntity>().eq("id",userId);
        UserEntity entity =  userDao.selectOne(queryWrapper);
        //用户增加免费次数
        int freeCount = entity.getFreecount();
        freeCount += count;
        if(freeCount>5){
            freeCount = 5;
        }
        entity.setFreecount(freeCount);
        userDao.update(entity,new UpdateWrapper<UserEntity>());

    }

    @Override
    public  void  addVipDay(long userId,int days){
        UserDao userDao = this.baseMapper;
        QueryWrapper<UserEntity> queryWrapper =  new QueryWrapper<UserEntity>().eq("id",userId);
        UserEntity entity =  userDao.selectOne(queryWrapper);
        Date oldDate = entity.getEndTime();
        if (oldDate == null ){
            oldDate = new Date();
        }
        Calendar c = Calendar.getInstance();
        c.setTime(oldDate);
        c.add(Calendar.DATE, days);
        Date newDate = c.getTime();
        entity.setEndTime(newDate);
        userDao.update(entity,new UpdateWrapper<UserEntity>());

    }


}